
#pragma once

class CHrkMesh
{
public:
	typedef std::vector<unsigned short> TriStrip_t;
	typedef std::vector<TriStrip_t> TriStripList_t;

	CHrkMesh();
	~CHrkMesh();

	static void CalcNodePathMatrixInv( IGameNode* pNode ,GMatrix& mat,TimeValue t=TIME_NegInfinity );
	static void CalcNodePathMatrix( IGameNode* pNode,GMatrix& mat,TimeValue t=TIME_NegInfinity );
	
	static void CalcInitNodePathMatrixInv( IGameNode* pNode ,GMatrix& mat,TimeValue t=TIME_NegInfinity );
	static void CalcInitNodePathMatrix( IGameNode* pNode,GMatrix& mat,TimeValue t=TIME_NegInfinity );

	void BuildTriStrips( IGameNode* pGameNode );
	void OutputTriStrips( CHrkStream& os,CHrkStream&log,IGameNode* pGameNode,bool bBoneNode );

	void MakeSubMeshes( IGameNode* pGameNode );
	void MakeMaxSubMeshes( IGameNode* pGameNode );
	IGameMaterial* FindMaxStdMaterial( IGameNode* gameNode,int maxMatId );
	void MakeMaxTriLists( CHrkStream& log,IGameNode* pGameNode);
	void MakeTriLists( CHrkStream& log,IGameNode* pGameNode);
	void BuildInitPose( IGameNode* pGameNode );
	void BuildSkinedVert( IGameNode* pGameNode );
	void OutputTriLists( CHrkStream& os,CHrkStream&log,IGameNode* pGameNode,bool bBoneNode );
	void OutputSkin( CHrkStream& os, CHrkStream& log, IGameNode* pGameNode );
	void BeginMeshOut( CHrkStream& os,CHrkStream&log, IGameNode* pGameNode );
	void EndMeshOut( CHrkStream& os,CHrkStream&log );
	void Clear();

	int		findVertex(const Point3 &vertex, const Point3 &norm, const Point2 &texcoord, const Point3 &color);
	
	//map old vert index to new vert index
	std::vector<std::vector<unsigned int>> m_vtxMap;

	IGameSkin* m_pIGameSkin;
	IGameMesh* m_pGameMesh;
	
	Mesh* m_maxMesh;
	Mtl* m_maxMtl;
	std::vector<FaceEx> m_maxFaceExVec;
	std::vector<Tab<Face*>> m_maxMatFacesTab;

	//skin vert index and weight grouped by each bone
	std::vector<IGameNode*> m_boneVec;
	std::vector<std::vector<int>>m_skinVertIndexVec;
	std::vector<std::vector<float>>m_skinVertWeightsVec;

	std::vector<Tab<FaceEx*>> m_matFacesTab;
	std::vector<IGameMaterial*> m_matTab;
	std::vector<TriStripList_t> m_triStripListVec;

	int m_nVerticesCount;

	std::vector<Point3> m_VerticesObjectSpace;
	std::vector<Point3> m_Vertices;
	std::vector<Point3> m_Normals;
	std::vector<Point2> m_Texcoords;
	std::vector<Point3> m_VertexColors;
	std::vector<float> m_VertexAlpha;
	std::vector<Point3> m_Tangents;
	std::vector<Point3> m_Binormals;

	std::vector<DWORD> m_FaceIndexes;	//3*faceNum

	BOOL m_bCalcInitPose;
};
